#include "server.h"

ThreadPool pool;

// 工作线程 ID 数组
pthread_t worker_tids[THREAD_POOL_SIZE];

// 初始化线程池
void thread_pool_init() {
    pool.capacity = TASK_QUEUE_CAPACITY;
    pool.front = pool.rear = 0;
    pthread_mutex_init(&pool.lock, NULL);
    sem_init(&pool.task_count, 0, 0);
}

// 添加任务到线程池
void thread_pool_add_task(int client_fd) {
    pthread_mutex_lock(&pool.lock);
    if ((pool.rear + 1) % pool.capacity != pool.front) {
        pool.queue[pool.rear].client_fd = client_fd;
        pool.rear = (pool.rear + 1) % pool.capacity;
        sem_post(&pool.task_count);
        printf("任务添加到线程池，当前任务队列长度: %d\n", (pool.rear - pool.front + pool.capacity) % pool.capacity);
    } else {
        fprintf(stderr, "任务队列已满，拒绝连接\n");
        close(client_fd);
    }
    pthread_mutex_unlock(&pool.lock);
}

// 清空线程池任务队列
void thread_pool_clear() {
    pthread_mutex_lock(&pool.lock);
    while (pool.front != pool.rear) {
        close(pool.queue[pool.front].client_fd);
        pool.front = (pool.front + 1) % pool.capacity;
    }
    pthread_mutex_unlock(&pool.lock);
}

// 工作线程处理函数
void* worker_thread(void *arg) {
    arg = NULL;
    while (1) {
        sem_wait(&pool.task_count);

        // 检查是否需要关闭
        if (shutdown_flag) {
            break;
        }

        pthread_mutex_lock(&pool.lock);
        Task task = pool.queue[pool.front];
        pool.front = (pool.front + 1) % pool.capacity;
        pthread_mutex_unlock(&pool.lock);

        // 处理客户端请求
        atomic_fetch_add(&active_connections, 1);
        printf("新连接 | 活跃连接数: %d\n", active_connections);

        int count = 0;
        char buffer[BUFFER_SIZE];
        memset(buffer, 0, sizeof(buffer)); // 初始化缓冲区
        while (1) {
            ssize_t len = recv(task.client_fd, buffer, sizeof(buffer) - 1, 0);
            if (len > 0) {
                // buffer[len] = '\0'; // 确保字符串以 '\0' 结尾
                // printf("收到客户端请求: %s\n", buffer);

                // 处理心跳包
                if (strcmp(buffer, "HEARTBEAT") == 0) {
                    ssize_t send_len = send(task.client_fd, "ACK", 3, 0);
                    if (send_len < 0) {
                        perror("发送心跳响应失败");
                    } else {
                        printf("成功发送心跳响应给客户端\n");
                    }
                    continue;
                }

                ssize_t send_len = send(task.client_fd, buffer, len, 0);
                if (send_len < 0) {
                    perror("发送响应失败");
                } else {
                    // printf("count = %d\n", count++);
                    printf("成功发送 %zd 字节数据给客户端\n", send_len);
                }
            } else if (len < 0) {
                perror("接收数据失败");
                break;
            } else {
                printf("客户端关闭连接\n");
                break;
            }
        }

        atomic_fetch_sub(&active_connections, 1);
        close(task.client_fd);
        printf("连接关闭 | 活跃连接数: %d\n", active_connections);
    }
    return NULL;
}

// 处理客户端请求
int server_receive_logic_manage() {
    int ret = 0;
    switch(1) {
        case 0:
            break;
        default:
            break;
    }
    return ret;
}